package com.webreport.webapp.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webreport.exception.LoginException;
import com.webreport.exception.ManagerException;
import com.webreport.model.db.SessionMonitor;
import com.webreport.service.SessionManager;
import com.webreport.utils.JsonUtils;
import com.webreport.utils.SpringContextUtil;
import com.webreport.webapp.bean.LoginBean;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.keygen.KeyGenerators;

public class SessionUtils {
	private static final String cookiename = "webreport";
	private static SessionUtils  utils =  new SessionUtils();
	private SessionManager manager;
	public SessionManager getSessionManager() {
		this.manager = (SessionManager) SpringContextUtil.getBean("sessionManager");
		return this.manager;
	}
	private SessionUtils(){}
	public static SessionUtils getInst(){
		return utils;
	}
	public SessionMonitor getSession(HttpServletRequest request){
		Cookie[] cookies= request.getCookies();
		for(Cookie c:cookies){
			if (StringUtils.equals(cookiename, c.getName())){
				String clientid = c.getValue();
				try {
					return this.getSessionManager().querySession(clientid);
				} catch (ManagerException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	public LoginBean getLogin(HttpServletRequest request){
		SessionMonitor session = this.getSession(request);
		if (session != null){
			if (session.getExpiretime() != null){
				if (session.getExpiretime().before(new java.util.Date())){
					return null;
				}
			}
			if (!StringUtils.isEmpty(session.getUser())){
				return (LoginBean) JsonUtils.fromJson(session.getUser(), LoginBean.class);
			}
		}	
		return null;
	}
	public void addSession(HttpServletRequest request,HttpServletResponse response, LoginBean login) throws ManagerException, LoginException{
		SessionMonitor s = this.getSessionManager().insertSession(login, request.getRemoteAddr());
		Cookie tokencookie = new Cookie(cookiename, s.getClientid());
		tokencookie.setPath(request.getContextPath());
		if (login.getExpiredays() >0){
			tokencookie.setMaxAge(login.getExpiredays() * 60 * 24);
		}
		response.addCookie(tokencookie);
	}
	public void removeSession(HttpServletRequest request,HttpServletResponse response) throws ManagerException{
		SessionMonitor s = this.getSession(request);
		if (s != null){
			this.getSessionManager().disableSession(s.getClientid());
			Cookie cookie = new Cookie(cookiename,null);
			cookie.setPath(request.getContextPath());
			cookie.setMaxAge(-1);
			response.addCookie(cookie);
		}
	}
	public static void main(String[] a){
		String t = "saltdsfsf";
		String salt = KeyGenerators.string().generateKey();
		byte[] t1 = Encryptors.standard("password", salt).encrypt(t.getBytes());
		//byte[] t2 = Encryptors.standard("password", "d").decrypt(t1);
		System.out.println(t);
		System.out.println(t1);
	}
}
